/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package midpmpc;

import java.util.Hashtable;

/**
 *
 * @author vvs
 */
public class MpdResponse {
    public String respStr;
    public Pair[] kvlist;
//    String[] keys;
//    String[] vals;
    public class Pair
    {
        public String key,val;
        public Pair(String k, String v){
            key=k;val=v;
        }
        public Pair(String str)
        {
            int dp=str.indexOf(": ");
            key=str.substring(0,dp);
            val=str.substring(dp+2);
        }
        public String toString()
        {
            return key+" = "+val;
        }

    }
    public MpdResponse(String resp) {
        respStr=resp;
        int lc=0;
        for (int i=0;i<resp.length();i++)
            if (resp.charAt(i)=='\n')
                lc++;
        kvlist=new Pair[lc];
        lc=0;
        String tLine="";
        char tc=' ';
        for (int i=0;i<resp.length();i++)
        {
            if ((tc=resp.charAt(i))=='\n'){
                kvlist[lc]=new Pair(tLine);
                tLine="";
                lc++;
            }
            else
                tLine+=tc;
        }
//        String ttt=resp;


    }
    public String getString(String name)
    {
        for (int i=0;i<kvlist.length;i++)
        {
            if (kvlist[i].key.equals(name))
                return kvlist[i].val;
        }
        return null;
    }
    public String toString()
    {
        String ret="";
        for (int i=0;i<kvlist.length;i++)
            ret+=(kvlist[i].toString()+'\n');
        return ret;
    }
    public FileInfo[] toFileList()
    {
        int fc=0;
        for (int i=0;i<kvlist.length;i++)
            if (kvlist[i].key.equals("file") | kvlist[i].key.equals("directory"))
                fc++;
        FileInfo[] ret=new FileInfo[fc];
        fc=-1;
        String k,v;
        for (int i=0;i<kvlist.length;i++)
        {
            k=kvlist[i].key;
            v=kvlist[i].val;
            if (k.equals("file"))
            {
                fc++;
                ret[fc]=new FileInfo();
                ret[fc].file=kvlist[i].val;
            }
            else if (k.equals("directory"))
            {
                fc++;
                ret[fc]=new FileInfo();
                ret[fc].file=kvlist[i].val;
                ret[fc].isDir=true;
            }
            else
            {
                if (k.equals("Artist"))
                    ret[fc].artist=v;
                else if (k.equals("Title"))
                    ret[fc].title=v;
                else if (k.equals("Album"))
                    ret[fc].album=v;
                else if (k.equals("Id"))
                    ret[fc].id=v;
                // TODO other fields
            }
        }
        return ret;
    }
    public void setVal(Hashtable storage,String srcName,String destName)
    {
        storage.put(destName, getString(srcName));
    }
    public void setVal(Hashtable storage,String keyName)
    {
        storage.put(keyName, getString(keyName));
    }
    public void copy(Hashtable storage)
    {
        String k,v;
        for (int i=0;i<kvlist.length;i++)
        {
            k=kvlist[i].key;
            v=kvlist[i].val;
            storage.put(k, v);
        }

    }


}
